###########################################
# (IN)DIRECT RULE IN FRENCH WEST AFRICA
#
# Replication Material for:
# Continuity or Change? (In)direct Rule in British and French Colonial Africa
# 
# Carl Mueller-Crepon, 2020
# International Organization
#
# File Description:
# Analysis of Elise Huillery's data on French taxation and investments
#
# Called from scripts/governance/analysis_budgetetal.R
#
###########################################

# Prepare data ####

# Sum of all taxes and investments
data.frnc$all.tax <- data.frnc$impind_p + data.frnc$achour_p + data.frnc$zekkat_p+ data.frnc$imp_rev
data.frnc$all.inv <- data.frnc$totneuf + data.frnc$tottrava

# Make cross-sectional data
agg.vars <- c(c("all.tax", "all.inv", "totensei","totperss"), main.expl, base.controls.abs, 
              ethnic.controls, nature.controls, "pop", "area")
frnc.cross <- aggregate.data.frame(data.frnc[,agg.vars],
                                   data.frnc[,c("colony", "geo.match")],
                                   FUN = function(x){ifelse(all(is.na(x)),NA, mean(x, na.rm = T))})

#########################################
# Absolute magnitudes
# Appendix Table A26
#########################################

# Init
stub <- "french.wa.abs"
dep.vars <- c("all.tax", "all.inv", "totensei","totperss")
titles <- c("Taxes","Public works","Teachers","Doctors")

# Estimation
this.m <- lapply(dep.vars, function(dv){
  return(felm(make_form(dv = paste0("log(",dv,"+1)"), expl = c(main.expl, base.controls.abs,  ethnic.controls,  nature.controls),
                        iv = "0", fe = fe.cross, se =  "geo.match"), data = frnc.cross))
})


# Output

# ... info
add.lines <- list(latex.any.row("Colony FE: ",rep("yes",length(this.m))),
                  latex.any.row("Baseline controls: ",rep("yes",length(this.m))),
                  latex.any.row("Nature controls: ", rep(c("yes"),length(this.m))),
                  latex.any.row("Ethnic controls: ", rep(c("yes"),length(this.m))),
                  latex.mean.dv(this.m))

# ... stargazer
fileConn<-file(paste0(tab.path, stub, ".tex"))
writeLines(stargazer(this.m,
                     title="Precolonial centralization and absolute local revenues \\& expenditures (logged): French West Africa",
                     dep.var.caption = "",dep.var.labels.include = FALSE,
                     column.labels = titles,
                     keep= keep.controls[1], covariate.labels = keep.labels[1],
                     notes.align = "l",label=paste0("tab.",stub),align =T,
                     add.lines = add.lines, 
                     digits = 3, intercept.top = T,intercept.bottom = F,
                     omit.stat = c("rsq","res.dev","ser"),column.sep.width = "5pt",
                     notes = latex.notes.add.appx(.87, sample = "The sample consists of all French colonies in West Africa."), notes.label = "", notes.append = F,
                     type  = output.type),
           fileConn)
close(fileConn)


#########################################
# Per capita magnitudes
# Appendix Table A27
#########################################

stub <- "french.wa.pc"
dep.vars <- c("all.tax", "all.inv", "totensei","totperss")
titles <- c("Taxes","Public works","Teachers","Doctors")

# Estimation
this.m <- lapply(dep.vars, function(dv){
  return(felm(make_form(dv = paste0("log((",dv,"+1)/pop)"), expl = c(main.expl, base.controls,  ethnic.controls,  nature.controls),
                        iv = "0", fe = fe.cross, se =  "geo.match"), data = frnc.cross))
})



# Output

# ... info
add.lines <- list(latex.any.row("Colony FE: ",rep("yes",length(this.m))),
                  latex.any.row("Baseline controls: ",rep("yes",length(this.m))),
                  latex.any.row("Nature controls: ", rep(c("yes"),length(this.m))),
                  latex.any.row("Ethnic controls: ", rep(c("yes"),length(this.m))),
                  latex.mean.dv(this.m))

# ... stargazer
fileConn<-file(paste0(tab.path, stub, ".tex"))
writeLines(stargazer(this.m,
                     title="Precolonial centralization and local revenues \\& expenditures per-capita (logged): French West Africa",
                     dep.var.caption = "",dep.var.labels.include = FALSE,
                     column.labels = titles,
                     keep= keep.controls[1], 
                     covariate.labels = keep.labels[1],
                     notes.align = "l",label=paste0("tab.",stub),align =T,
                     add.lines = add.lines, 
                     digits = 3, intercept.top = T,intercept.bottom = F,
                     omit.stat = c("rsq","res.dev","ser"),column.sep.width = "5pt",
                     notes = latex.notes.add.appx(.85, sample = "The sample consists of all French colonies in West Africa."), notes.label = "", notes.append = F,
                     type  = output.type),
           fileConn)
close(fileConn)

